<!DOCTYPE html>
<html>

	<head>
		<meta charset="UTF-8">
		<title></title>
		<link rel="stylesheet" type="text/css" href="../../../Themes/default/easyui.css">
		<link rel="stylesheet" type="text/css" href="../../../Themes/icon.css">
		<link rel="stylesheet" type="text/css" href="../../../CSS/demo.css">
		<link rel="stylesheet" type="text/css" href="../../../CSS/common.css">
	</head>

	<body style="padding: 0px;">
		<div id="toolbar" class="receipts_toolbar_style"></div>
		<div>
			<form id="ff" method="post">
				<div style="margin-left: 15%;padding: 30px 0px;">
					<table cellpadding="5" style="width: 85%;">
						<tr>
							<td width="10%" style="display: none">合同ID</td>
							<td width="23%" style="display: none">
								<input style="width:100%" class="easyui-textbox" type="text" id="input_ID" name="ID" data-options="required:true,prompt:'由系统自动生成，不需填写',disabled:true"></input>
							</td>
							<td width="10%">合同编号</td>
							<td width="23%">
								<input style="width:100%" class="easyui-textbox" id="input_ORDER_NO" name="ORDER_NO" data-options="required:true,prompt:'由系统自动生成，不需填写',disabled:true"></input>
							</td>
							<td width="10%">客户名称</td>
							<td width="23%">
								<input style="width:100%" class="easyui-combobox" type="text" id="input_TENANT_NAME" name="TENANT_NAME" data-options="required:true"></input>
							</td>
							<td width="10%">合同年限</td>
							<td width="23%">
								<input style="width:100%" class="easyui-numberbox" type="text" suffix="年" id="input_CONTRACT_LIFE" name="CONTRACT_LIFE" data-options="required:true,prompt:'由系统自动生成，不需填写',disabled:true"></input>
							</td>
						</tr>
						<tr>
							<!--<td width="10%">合同期限</td>
							<td width="23%">
								<input style="width:100%" class="easyui-textbox" type="contract_term" id="input_DEADLINE" name="DEADLINE" data-options="required:true"></input>
							</td>-->
							<td width="10%">开始时间</td>
							<td width="23%">
								<input style="width:100%" class="easyui-datebox" required data-options="validType:'md[\'10/11/2015\']'" id="input_BEGIN_TIME" name="BEGIN_TIME"></input>
							</td>
							<td width="10%">结束时间</td>
							<td width="23%">
								<input style="width:100%" class="easyui-datebox" required data-options="validType:'md[\'10/11/2015\']'" id="input_END_TIME" name="END_TIME"></input>
							</td>
							<td width="10%">总经营面积</td>
							<td width="23%">
								<input style="width:100%" class="easyui-numberbox" data-options="required:true,disabled:true" id="input_BUSINESS_AREA" name="BUSINESS_AREA" suffix=" m&sup2"></input>
							</td>
						</tr>
						<tr>
							<td width="10%">租金总额</td>
							<td width="23%">
								<input style="width:100%" class="easyui-numberbox" id="input_RENT_SUM" name="RENT_SUM" data-options="required:true,disabled:true" prefix="￥"></input>
							</td>
							<td width="10%">保证金总额</td>
							<td width="23%">
								<input style="width:100%" class="easyui-numberbox" type="text" id="input_MARGIN_SUM" name="MARGIN_SUM" data-options="required:true,disabled:true" prefix="￥"></input>
							</td>
							<td width="10%">其他费用合计</td>
							<td width="23%">
								<input style="width:100%" class="easyui-numberbox" type="text" id="input_OTHERS_SUM" name="OTHERS_SUM" data-options="required:true,disabled:true" prefix="￥"></input>
							</td>
						</tr>
					</table>
				</div>
				<table id="tt">

				</table>
				<div style="padding: 10px 0px;">
					<table>
						<tr>
							<th>人民币金额合计</th>
							<td>
								<input type="text" style="border: none;" name="number" />
							</td>
							<th>人民币金额合计(大写)</th>
							<td width="auto">
								<input type="text" style="border: none;" id="dx" />
							</td>
						</tr>
					</table>
				</div>
				<div id="new_footer" style="margin-left: 23%;margin-top: 50px;"></div>
			</form>
		</div>
		<div id="dlg" class="hiddenDialog">
			<iframe id="ifrmEdit" style="width: 100%;height: 100%;" frameborder="0" scrolling="no"></iframe>
		</div>
		<script type="text/javascript" src="../../../JS/jquery.min.js"></script>
		<script type="text/javascript" src="../../../JS/jquery.easyui.min.js"></script>
		<script type="text/javascript" src="../../../JS/finance.js"></script>
		<script type="text/javascript" src="../../../JS/jquery.edatagrid.js"></script>
		<script type="text/javascript" src="../../../JS/common.js"></script>
		<script type="text/javascript" src="../../../JS/MarketShareFunction/contractShareFunction.js"></script>
	</body>
	<script type="text/javascript">
		var currid = AddFlag; //当前页面的ID
		var id = getID(); //获取上个页面传回的ID
		var currPage = 0; //当前的页数
		var MaxPage = 0;
		var MinPage = 1;
		var isVerify;
		var tenant_id;
		var AddDialog;
		var currRow;
		var chargCount = 0;
		var typeInfo = new Array(); //将typeInfo初始化为数组
		var a = new Object();
		var _a = JSON.stringify(a);
		localStorage.setItem("longTermContractData", _a); //初始化合同数据
		var _detailToolbarData;
		_detailToolbarData = AddDetailToolBar(7);

		var data = [{
			"icon": "icon-responsibilities",
			"func": "dutyFile()",
			"functionname": "卡证及责任书"
		}];

		//处理客户名称的combobox
		$('#input_TENANT_NAME').combobox({
			valueField: 'NAME',
			textField: 'NAME',
			panelHeight: 'auto',
			onChange: function(newValue, oldValue) {
				if(newValue != "" && newValue != undefined) {
					var url = RemoteLinkAddress + "/market/tenant/findbypinyin?PINYINFIELDNAME=" + newValue.trim();
					$(this).combobox("reload", url);
				}
			},
			onSelect: function(record) {
				console.log(record.ID);
				tenant_id = record.ID;
			}
		});

		$("#input_END_TIME").datebox({
			valueField: 'BEGIN_TIME',
			textField: 'BEGIN_TIME',
			onSelect: function(date) {
				
				var begintime = $("input[name='BEGIN_TIME']").val();
				var endtime = $("input[name='END_TIME']").val();
				var peroid = CalTimeLimit(begintime, endtime, TIME_YEAR);
				$("#input_CONTRACT_LIFE").numberbox('setValue', peroid);
			}
		})

		content = AddReceiptsToolBar(0, data);
		$("#toolbar").append(content); /*在ID=toolbar的标记后面添加content中的内容*/

		GetTable();
		//      $('#tt').edatagrid("iniRow");
		$(document).ready(function(){
			if(id != AddFlag && id != undefined) {
				currid = id;
				loadRemote();
			}
		});
		if(id == undefined) {
			currid = AddFlag;
			$('#tt').edatagrid("iniRow");
		}
		///增加脚注
		getPageMaxAndMin();
		addFooter($("#new_footer"));
		//增加单据的toolba
		///单据相关功能
		function save() {
			if($("input[name='CONTRACT_NO']").val() == "") {
				$.messager.alert('提示信息', '合同信息不能为空!');
			} else {
				SaveData();
			}
		}
		
		function print() {
			var pLocation = "";
			if(currid == AddFlag) {
				$.messager.alert("错误", "当前页面为新增页面不能打印");
				return;
			} else {
				pLocation = "BIContractPrint.html?id=" + currid;
				window.open(pLocation);
			}
		}

		function newdata() {
			currid = AddFlag;
			currPage = 0;
			$('#ff').form('reset');
		}

		function searchmore() {
			AddTabIn("合同查询", parent.$('#tabs'), "UI/Market/BIContract/BIContractSearch.html");
		}

		function getRemoteData(_url) //统一获取远程的数据
		{
			$.ajax({ //调用ajax方法，从服务器获取到该订单的所有数据
				type: "get",
				url: _url,
				dataType: "json", //服务器返回的数据类型
				success: function(result) {
					if(result != null) {
						getFormData(result);
						currid = result.ID;
						currPage = result.ID;
						isVerify = result.ISVERIFY;
						var _data = result.rowsBoost;
						$('#tt').datagrid('loadData', _data);
					}

				}, //请求成功后，接受服务器传来的Json字符串，并做相应的处理
				error: function(xhr, status, error) {
					$.messager.alert("错误", "请求失败!");
				}
			});
		}
		//////////
		function loadRemote() { //载入服务器上的数据，用于修改以及查看更多的时候调用，根据id是否等于-1来判断
			var _url = RemoteLinkAddress + '/market/contract/findbyid.do?ID=' + currid;
			getRemoteData(_url);
		}

		$('#input_sum').textbox({
			onChange: function(value) {
				var _trim = $.trim(value);
				var s = changeMoneyToChinese(_trim);
				$('#span_sum').html(s);
			}
		});

		function GetTable() {
			$('#tt').edatagrid({
				height: 280,
				//				width: '100%',
				title: '长期合同明细',
				toolbar: _detailToolbarData,
				singleSelect: 'true', //是否单选：是
				//				rownumbers: 'true',
				//				fitColumns: 'true', //自动调整列：是
				//				showFooter: 'true',
				autoSave: 'true',
				columns: [
					[{
						field: 'ID',
						title: 'ID',
						width: '0%',
						align: 'center',
					}, {
						field: 'BOOST_ID',
						title: '摊位ID',
						width: '0%',
						align: 'center',
						editor: {
							type: 'textbox',
							options: {
								valueField: "BOOST_ID",
								textField: "BOOST_ID",
								editable: false,
							}
						}
					}, {
						field: 'REGION_TYPE',
						title: '区域类型',
						width: '8%',
						align: 'center',
						editor: {
							type: 'combobox',
							options: {
								url: RemoteLinkAddress + "/market/regiontype/listall.do",
								valueField: "NAME",
								textField: "NAME",
								editable: false,
								required: true,
								onSelect: function(record) {
									var _url = RemoteLinkAddress + "/market/region/listall.do?TYPE_ID=" + record.ID;
									var rowIndex = $("#tt").datagrid('getRowIndex', currRow);
									var target = $('#tt').datagrid('getEditor', {
										'index': rowIndex,
										'field': 'REGION_NAME'
									}).target;
									target.combobox('clear');
									target.combobox('reload', _url);
								}
							}
						}
					}, {
						field: 'REGION_NAME',
						title: '区域名称',
						width: '8%',
						align: 'center',
						editor: {
							type: 'combobox',
							options: {
								valueField: "NAME",
								textField: "NAME",
								editable: false,
								required: true,
								mode: 'remote',
								method: 'get',
								onSelect: function(record) {
									var rowIndex = $("#tt").datagrid('getRowIndex', currRow);
									var target = $('#tt').datagrid('getEditor', {
										'index': rowIndex,
										'field': 'REGION_TYPE'
									}).target;
									var curr_Type = target.combobox('getText');
									var _url = RemoteLinkAddress + "/market/boost/listall.do?REGION_TYPE=" + curr_Type + "&REGION_NAME=" + record.NAME;
									target = $('#tt').datagrid('getEditor', {
										'index': rowIndex,
										'field': 'HOUSE_NUMBER'
									}).target;
									target.combobox('clear');
									target.combobox('reload', _url);
								}
							}
						}
					}, {
						field: 'HOUSE_NUMBER',
						title: '摊位门牌号',
						width: '8%',
						align: 'center',
						editor: {
							type: 'combobox',
							options: {
								valueField: "HOUSE_NUMBER",
								textField: "HOUSE_NUMBER",
								editable: true,
								required: true,
								onSelect: function(record) {
									var rowIndex = $("#tt").datagrid('getRowIndex', currRow);
									if(record.STATE != "空闲") {
										$.messager.alert("警告", "该摊位已被租用！");
										var target = $('#tt').datagrid('getEditor', {
											'index': rowIndex,
											'field': 'HOUSE_NUMBER'
										}).target;
										target.textbox('clear');
										target.textbox('setValue', "");
										var target = $('#tt').datagrid('getEditor', {
											'index': rowIndex,
											'field': 'BUSINESS_AREA'
										}).target;
										target.textbox('clear');
										target.textbox('setValue', "");
										return;
									}

									var target = $('#tt').datagrid('getEditor', {
										'index': rowIndex,
										'field': 'BOOST_ID'
									}).target;
									target.textbox('clear');
									target.textbox('setValue', record.ID);
									var target = $('#tt').datagrid('getEditor', {
										'index': rowIndex,
										'field': 'BUSINESS_AREA'
									}).target;
									target.textbox('clear');
									target.textbox('setValue', record.AREA);
								}
							}
						}
					}, {
						field: 'BUSINESS_AREA',
						title: '经营面积',
						width: '8%',
						align: 'center',
						editor: {
							type: 'textbox',
							options: {
								editable: false,
								required: true
							}
						}
					}, {
						field: 'RENT',
						title: '租金',
						width: '8%',
						align: 'center',
						editor: {
							type: 'numberbox',
							options: {
								required: true
							}
						}
					}, {
						field: 'MARGIN',
						title: '保证金',
						width: '8%',
						align: 'center',
						editor: {
							type: 'numberbox',
							options: {
								required: true
							}
						}
					}, {
						field: 'OTHERS_CHARGE',
						title: '其他费用',
						width: '8%',
						align: 'center',
						editor: {
							type: 'numberbox',
							options: {
								required: true
							}
						}
					}, {
						field: 'BEGIN_TIME',
						title: '合同开始时间',
						width: '8%',
						align: 'center',
						editor: {
							type: 'datebox',
							options: {
								required: true
							}
						}
					}, {
						field: 'END_TIME',
						title: '合同结束时间',
						width: '8%',
						align: 'center',
						editor: {
							type: 'datebox',
							options: {
								required: true
							}
						}
					}, {
						field: 'RENT_END',
						title: '实际租用结束时间',
						width: '8%',
						align: 'center',
						editor: {
							type: 'datebox',
							options: {
								required: true,
								editable: false,
								disabled: true
							}
						}
					}, {
						field: 'SUM',
						title: '小计',
						width: '8%',
						align: 'center',
						editor: {
							type: 'numberbox',
							options: {
								required: false,
								editable: false,
							}
						}
					}, {
						field: 'CHARGE',
						title: '摊位收费',
						width: '6%',
						align: 'center',
						formatter: function(value, row, index) {
							return '<a href="#" onclick="charge(' + row.BOOST_ID + ')">收费</a>';
						}
					}]
				],
				onDblClickRow: function(index, row) {
					currRow = row;
				},
				onAdd: function(index, row) {
					currRow = row;
				},
				onEndEdit: function(index, row, changes) {
					var sum = parseFloat(row.RENT) + parseFloat(row.MARGIN) + parseFloat(row.OTHERS_CHARGE);
					$('#tt').edatagrid('updateRow', {
						index: index,
						row: {
							SUM: sum
						}
					});
					$('#tt').edatagrid('refreshRow', index);
					autoCalculate();
				}
			});
			$("#tt").datagrid('hideColumn', 'ID');
			$("#tt").datagrid('hideColumn', 'BOOST_ID');
		}

		function getData() {
			var data = JSON.parse(localStorage.getItem("longTermContractData"));
			data.TENANT_NAME = $("input[name='TENANT_NAME']").val();
			data.BEGIN_TIME = $("input[name='BEGIN_TIME']").val();
			data.END_TIME = $("input[name='END_TIME']").val();
			data.BUSINESS_AREA = $("input[name='BUSINESS_AREA']").val();
			data.RENT_SUM = $("input[name='RENT_SUM']").val();
			data.MARGIN_SUM = $("input[name='MARGIN_SUM']").val();
			data.OTHERS_SUM = $("input[name='OTHERS_SUM']").val();
			data.CONTRACT_LIFE = $("input[name='CONTRACT_LIFE']").val();
			data.USERNAME = "admin";
			data.TENANT_ID = tenant_id;
			data.CONTRACT_TYPE = "新建";
			data.ORDER_TYPE = "1"; //1代表长期
			var dataGridRows = $("#tt").datagrid("getRows");
			data.rowsBoost = new Array();
			for(var i = 0; i < dataGridRows.length; i++) {
				if(typeof(dataGridRows[i].BOOST_ID) != "undefined") {
					var _index = i;
					data.rowsBoost[_index] = new Object();
					data.rowsBoost[_index].BOOST_ID = dataGridRows[i].BOOST_ID;
					data.rowsBoost[_index].REGION_NAME = dataGridRows[i].REGION_NAME;
					data.rowsBoost[_index].REGION_TYPE = dataGridRows[i].REGION_TYPE;
					data.rowsBoost[_index].HOUSE_NUMBER = dataGridRows[i].HOUSE_NUMBER;
					data.rowsBoost[_index].BUSINESS_AREA = dataGridRows[i].BUSINESS_AREA;
					data.rowsBoost[_index].RENT = dataGridRows[i].RENT;
					data.rowsBoost[_index].BEGIN_TIME = dataGridRows[i].BEGIN_TIME;
					data.rowsBoost[_index].END_TIME = dataGridRows[i].END_TIME;
					data.rowsBoost[_index].MARGIN = dataGridRows[i].MARGIN;
					data.rowsBoost[_index].OTHERS_CHARGE = dataGridRows[i].OTHERS_CHARGE;
					data.rowsBoost[_index].SUM = dataGridRows[i].SUM;
				}
			}
			localStorage.setItem("longTermContractData", JSON.stringify(data));
			//			localStorage.longTermContractData = JSON.stringify(data);
			//			submitData(data);
		}

		function AddDict() {
			var pLocation = "UI/COMMON/DictData.html?id=-1";
			$("#ifrmEdit").attr("src", pLocation);
			constructDialog($("#dlg"), "添加基础信息", 400, 300);
			AddDialog.dialog('open');
		}

		function charge(id) {
			var a = checkDataIntegrity();
			if(a === 0) {
				$.messager.alert("警告", "页面数据不完整!");
				return;
			}
			chargCount++;
			var pLocation = "UI/Market/BIContractCreate/BIContractAddCharge.html?id=" + id;
			$("#ifrmEdit").attr("src", pLocation);
			constructDialog($("#dlg"), "新建长期合同收费管理", 600, 310);
			AddDialog.dialog('open');
		}

		function checkDataIntegrity() {
			var customername = $("input[name='CUSTOMER_NAME']").val();
			var begintime = $("input[name='BEGIN_TIME']").val();
			var endtime = $("input[name='END_TIME']").val();
			var businessarea = $("input[name='BUSINESS_AREA']").val();
			var rentsum = $("input[name='RENT_SUM']").val();
			var marginsum = $("input[name='MARGIN_SUM']").val();
			var otherssum = $("input[name='OTHERS_SUM']").val();
			var rowsdata = $("#tt").datagrid('getRows');
			if(customername != "" && begintime != "" && endtime != "" && businessarea != "" && rentsum != "" && marginsum != "" && otherssum != "") {
				if(rowsdata.length != 0) {
					for(var i = 0; i < rowsdata.length; i++) {
						var region = rowsdata[i].REGION;
						var regiontype = rowsdata[i].REGION_TYPE;
						var housenumber = rowsdata[i].HOUSE_NUMBER;
						var businessarea = rowsdata[i].BUSINESS_AREA;
						var rent = rowsdata[i].RENT;
						var margin = rowsdata[i].MARGIN;
						var othercharge = rowsdata[i].OTHERS_CHARGE;
						var begintime = rowsdata[i].BEGIN_TIME;
						var endtime = rowsdata[i].END_TIME;
						if(region == "" || regiontype == "" || housenumber == "" || businessarea == "" || rent == "" || margin == "" || othercharge == "" || begintime == "" || endtime == "") {
							return 0;
						}
					}
				} else {
					return 0;
				}
			} else {
				return 0;
			}
		}

		function dutyFile() {
			getData();
			if(chargCount === 0) {
				$.messager.alert("警告", "请完成明细中的收费操作！");
				return;
			}
			var datagridData = $("#tt").datagrid('getRows');
			var datagridLength = 0;
			for(var data of datagridData) {
				if(typeof(data.BOOST_ID) != "undefined") {
					datagridLength++;
				}
			}
			if(chargCount < datagridLength) { //表格中的摊位没有全部点击收费按钮进行收费
				$.messager.alert("警告", "请完成表格中摊位的收费");
				return;
			}
			AddTabIn("新建长期合同收费管理", parent.$('#tabs'), "UI/Market/BIContractCreate/BIContractAddCardAndDutyFile.html?id=-1");
			//			var pLocation = "UI/Market/BIContractCreate/BIContractAddCardAndDutyFile.html?id=-1";
			//			$("#ifrmEdit").attr("src", pLocation);
			//			constructDialog($("#dlg"), "添加卡证及责任书", 800, 380);
			//			AddDialog.dialog('open');
		}
	</script>

</html>